//
//  MYTransferredInViewController.m
//  投融保
//
//  Created by 523 on 13-12-19.
//  Copyright (c) 2013年 木叶科技. All rights reserved.
//

#import "MYTransferredInVC.h"
#import "MYWebService.h"
#import "MBProgressHUD.h"
#import "MYRecommendViewController.h"

@interface MYTransferredInVC ()

@end

@implementation MYTransferredInVC
@synthesize bgScrollView;
@synthesize dictHuaRu;

extern NSDictionary *userInformation;

extern NSString *key;
extern NSString *appKey;


int prewTag ;  //编辑上一个UITextField的TAG,
float prewMoveY; //编辑的时候移动的高度

//用户划入金额，
extern NSString *moneyhuaru;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    NSLog(@"用户信息：------%@",userInformation);
    //关闭键盘手势
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgtouchesBegan)];
    self.bgScrollView.contentSize=CGSizeMake(320, SCREEN_HEIGHT+100);
    [self.bgScrollView addGestureRecognizer:tapGesture];
    
    self.lblUserName.text=[userInformation valueForKey:@"realName"];
    
    self.bankImage.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[userInformation valueForKey:@"banklog"]]]];
    
    
    self.lblCardNo.text=[userInformation valueForKey:@"bankCardNo"];
    self.txtPayPassWord.leftView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 44)];
    [self.txtPayPassWord setLeftViewMode:UITextFieldViewModeAlways];
    
    
    self.lblMoney.text=[NSString stringWithFormat:@"%@ 元",moneyhuaru];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnReturnClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//确认提交事件
- (IBAction)btnSubmitClick:(UIButton *)sender {
    
    if (self.lblMoney.text.length>=1&&self.txtPayPassWord.text.length>0) {
        //划款
        //    NSString *strDeductService=@"service=deductService&amount=50000&password=123qwe";
        NSString *strDeductService=[NSString stringWithFormat:@"service=deductService&amount=%@&password=%@",moneyhuaru,self.txtPayPassWord.text];
        
        MBProgressHUD *mbHud = [[MBProgressHUD alloc] initWithView:self.view];
        
        mbHud.labelText = @"正在加载";
        
        [self.view addSubview:mbHud];
        
        [mbHud showAnimated:YES whileExecutingBlock:^{
            //z菊花转动的时候 开始加载数据
        
            dictHuaRu=[NSDictionary dictionaryWithDictionary:[[MYWebService new] getDataSynWithKey:key encryptedString:strDeductService appKey:appKey]];
            
        } completionBlock:^{
            
            //执行加载数据完成后的操作。
            if ([[dictHuaRu objectForKey:@"code"] intValue]==0) {
                NSString *cancel=@"确定";
                NSString *message=[dictHuaRu objectForKey:@"message"];
                ALERTVIEW(message, cancel);
            }else{
                [self transferredResultWithCode:dictHuaRu];
            }
        }];
        
    }
}

- (IBAction)closeKeyboard:(UITextField *)sender {
    
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    [self.view endEditing:YES];
    
}

- (void)transferredResultWithCode:(NSDictionary *)dictResult
{
    [self.bgScrollView removeFromSuperview];
    NSString *str1,*str2,*str3,*str4;
    if ([[dictHuaRu objectForKey:@"code"] intValue]==1) {
        str1=@"划入成功";
        str2=@"继续划入";
        str3=[dictHuaRu objectForKey:@"message"];
        str4=@"happyface";
    }else{
        str1=@"划入失败";
        str2=@"重新划入";
        str3=[dictHuaRu objectForKey:@"message"];
        str4=@"Failed";
    }
    self.lblTitle.text=str1;
    Y_=0;
    if (IOS_VERSION>=7) {
        Y_=20;
    }
    UIView *viewTransferredscceuss=[[UIView alloc] initWithFrame:CGRectMake(0, 104+Y_, SCREEN_WIDTH, SCREEN_HEIGHT-Y_)];
    [viewTransferredscceuss setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:viewTransferredscceuss];
    
    
    UIImageView *imageFace=[[UIImageView alloc] initWithFrame:CGRectMake(240, 200, 60, 60)];
    imageFace.image=[UIImage imageNamed:str4];
    [viewTransferredscceuss addSubview:imageFace];
    
    UILabel *lblResult=[[UILabel alloc] initWithFrame:CGRectMake(320, 215, 200, 30)];
    lblResult.text=str3;
    lblResult.backgroundColor=[UIColor clearColor];
    lblResult.textColor=UIColorFromRGB(0xb70100);
    lblResult.textAlignment=NSTextAlignmentCenter;
    lblResult.font=[UIFont systemFontOfSize:24];
    [viewTransferredscceuss addSubview:lblResult];
    
    //继续划入
    UIButton *btnReTrans=[[UIButton alloc] initWithFrame:CGRectMake(54, 350, 300, 88)];
    [btnReTrans setBackgroundImage:[UIImage imageNamed:@"login_bg"] forState:UIControlStateNormal];
    [btnReTrans setTitle:str2 forState:UIControlStateNormal];
    [btnReTrans setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnReTrans.titleLabel setFont:[UIFont systemFontOfSize:26]];
    [btnReTrans addTarget:self action:@selector(btnReturnClick:) forControlEvents:UIControlEventTouchUpInside];
    [viewTransferredscceuss addSubview:btnReTrans];
    //返回首页
    UIButton *btnReturnHomepage=[[UIButton alloc] initWithFrame:CGRectMake(414, 350, 300, 88)];
    [btnReturnHomepage setBackgroundImage:[UIImage imageNamed:@"register_bg"] forState:UIControlStateNormal];
    [btnReturnHomepage setTitle:@"返回首页" forState:UIControlStateNormal];
    [btnReturnHomepage setTitleColor:UIColorFromRGB(0xb70100) forState:UIControlStateNormal];
    [btnReturnHomepage.titleLabel setFont:[UIFont systemFontOfSize:26]];
    [btnReturnHomepage addTarget:self action:@selector(btnReturnHomepageAction) forControlEvents:UIControlEventTouchUpInside];
    [viewTransferredscceuss addSubview:btnReturnHomepage];
}



#pragma mark +++++++++键盘+++++++++++
/**
 开始编辑UITextField的方法
 */
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect textFrame =  textField.frame;
    float textY = textFrame.origin.y+textFrame.size.height;
    float bottomY = self.bgScrollView.frame.size.height-textY;
    if(bottomY>=245)  //判断当前的高度是否已经有216，如果超过了就不需要再移动主界面的View高度
    {
        prewTag = -1;
        return;
    }
    prewTag = textField.tag;
    float moveY;
    if (IOS_VERSION>=7) {
        moveY = 245-bottomY;
    }else
    {
        moveY = 300-bottomY;
    }
    
    prewMoveY = moveY;
    
    NSTimeInterval animationDuration = 0.250f;
    CGRect frame = self.view.frame;
    frame.origin.y -=moveY;//view的Y轴上移
    frame.size.height +=moveY; //View的高度增加
//    self.bgScrollView.frame = frame;
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
//    self.bgScrollView.frame = frame;
    [self.bgScrollView setContentOffset:CGPointMake(0, 100)];
    [UIView commitAnimations];//设置调整界面的动画效果
    
}

/**
 结束编辑UITextField的方法，让原来的界面还原高度
 */
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
    
    if(prewTag == -1) //当编辑的View不是需要移动的View
    {
        return;
    }
    float moveY ;
    NSTimeInterval animationDuration = 0.250f;
    CGRect frame = self.bgScrollView.frame;
    if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
    {   //还原界面
        moveY =  prewMoveY;
        frame.origin.y +=moveY;
        frame.size. height -=moveY;
//        self.view.frame = frame;
        [self.bgScrollView setContentOffset:CGPointMake(0, 0)];
    }
    //bgScrollView移回原位置
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
//    self.bgScrollView.frame = frame;
    
    [UIView commitAnimations];
    [textField resignFirstResponder];
    
}

- (void)bgtouchesBegan
{
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    [self.view endEditing:YES];
}

- (void)btnReturnHomepageAction
{
//    [self presentModalViewController:[MYRecommendViewController new] animated:NO];
    [self presentViewController:[MYRecommendViewController new] animated:YES completion:nil];
}

#pragma mark-   -----------状态栏底色------
- (UIStatusBarStyle)preferredStatusBarStyle
{
    [self.view setBackgroundColor:[UIColor blackColor]];
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
@end
